﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Entity;
namespace DAL
{
    public class NewsClass
    {
        DbCommon.DbHelper DbHelper = new DbCommon.DbHelper();
        /// <summary>
        /// 添加类
        /// </summary>
        /// <param name="newsModel">newsclass模型</param>
        /// <returns>返回受影响的行数</returns>
       public int AddClass(Model.NewsClass newsModel){
           int i = 0;
           SqlParameter[] pr = new SqlParameter[] {
                new SqlParameter("@Title",newsModel.Title),
                new SqlParameter("@IsShow",newsModel.IsShow),
           };
           string sqlstr = "Insert into NewsClass (NCTitle,NCIsShow) values(@Title,@IsShow)";
           i=DbHelper.ExcuteNoQuery(sqlstr,pr);
           return i;
       }
        /// <summary>
        /// 更新类别
        /// </summary>
       /// <param name="newsModel">newsclass模型</param>
        /// <returns>返回受影响的行数</returns>
       public int updateClass(Model.NewsClass newsModel) {
           int i = 0;
           SqlParameter[] pr = new SqlParameter[]{
                new SqlParameter("@Title",newsModel.Title),
                new SqlParameter("@id",newsModel.id)
           };
           string sqlstr = "update NewsClass set NCTitle = @Title where NCId = @id";
           i = DbHelper.ExcuteNoQuery(sqlstr,pr);
           return i;
       }
        /// <summary>
        /// 根据id来删除一条数据
        /// </summary>
        /// <param name="id">id值</param>
        /// <returns>返回删除的行数</returns>
       public int DelClass(string id) {
           int i = 0;
           SqlParameter[] pr = new SqlParameter[]{
                new SqlParameter("@id",id)
           };
           string sqlstr = "Delete from NewsClass where NCId = @id";
           i = DbHelper.ExcuteNoQuery(sqlstr,pr);
           return i;
       }
       /// <summary>
       /// 获取NewsClass的表中的数据
       /// </summary>
       /// <returns>NewsClass表中的数据</returns>
       public DataTable GetNewsClass() {
           DataTable dt =new DataTable();
           string sqlstr = "select * from NewsClass";
           dt =DbHelper.ExcuteQuery(sqlstr);
           return dt;
       }
        /// <summary>
        /// 得到父类子类排序后的列表
        /// </summary>
        /// <returns>返回排序后的表</returns>
       //public DataTable GetOrderNewsClass(int ParentId)
       //{
       //    DataTable NewDt = new DataTable();
       //    DataTable OldDt = GetNewsClass();
       //    //将原来的表的结构复制给新表
       //    NewDt = OldDt.Clone();
       //    OrderContestClass(NewDt, OldDt, ParentId);
       //    return NewDt;
       //}
       /// <summary>
       /// 按照父类子类顺序排序
       /// </summary>
       /// <param name="NewDt">具有NewsClass原结构的新表</param>
       /// <param name="OldDt">在数据库中的ContestClass的表</param>
       /// <param name="ParentId">父类ID</param>
       //public void OrderContestClass(DataTable NewDt, DataTable OldDt, int ParentId) {
       //    //将空格转化为char
       //    char nbsp = (char)0xA0;
       //    DataRow[] dr = OldDt.Select("ParentId ="+ParentId);
       //    for (int i = 0; i < dr.Length; i++) {
       //        DataRow datarow = NewDt.NewRow();
       //        datarow["id"] = dr[i]["id"];
       //        if (Convert.ToInt32(dr[i]["ClassLayer"]) != 0)
       //        {
       //            datarow["Title"] = DbCommon.Utils.stringOfChar(Convert.ToInt32(dr[i]["ClassLayer"]), nbsp) + "┗" + dr[i]["Title"];
       //        }
       //        else
       //            datarow["Title"] = dr[i]["Title"];
       //        datarow["ParentId"] = dr[i]["ParentId"];
       //        datarow["ClassLayer"] = dr[i]["ClassLayer"];
       //        NewDt.Rows.Add(datarow);
       //        //调用自身迭代
       //        OrderContestClass(NewDt, OldDt, int.Parse(datarow["id"].ToString()));
       //    }
       //}
        /// <summary>
        /// 根据id和列名获取此行某个特定值
        /// </summary>
        /// <param name="id">id号</param>
        /// <returns>返回长发的ClassLayer</returns>
       public object getCell(int id,string Column ) {
           object cell = DbHelper.GetSingleItem("NewsClass", Column, " NCId= " + id);
           if (cell != null)
           {
               return cell;
           }
           else {
               return null;
           }
       }
        /// <summary>
        /// 向NewsClass中添加一行数据
        /// </summary>
        /// <param name="Title">标题</param>
        /// <param name="ParentId">父Id</param>
        /// <param name="ClassLayer">类别所属层</param>
        /// <returns></returns>
       public int AddContestClass(string Title,bool IsShow) { 
           int i = 0;
           SqlParameter[] pr = new SqlParameter[]{
                new SqlParameter("@Title",Title),
                new SqlParameter("@IsShow",IsShow),
            };
           string sqlstr = "insert into NewsClass (NCTitle,NCIsShow) values(@Title,@IsShow) ";
            i = DbHelper.ExcuteNoQuery(sqlstr, pr);
            return i;
       }

       /// <summary>
       /// 修改一条数据
       /// </summary>
       /// <param name="model">新闻模型</param>
       public int UpdateRow(BoNewsClass model)
       {
           System.Text.StringBuilder sql = new System.Text.StringBuilder();
           sql.Append("UPDATE [NewsClass] SET ");
           sql.Append("NCTitle=@NCTitle");
           sql.Append(",NCIsShow=@NCIsShow");
           sql.Append(" where NCId = @NCId ");
           return DbHelper.ExcuteNoQuery(sql.ToString(), GetParameters(model));
       }

       /// <summary>
       /// 添加一条记录
       /// </summary>
       /// <param name="model">实体类</param>
       /// <returns></returns>
       public int Insert( BoNewsClass model)
       {
           System.Text.StringBuilder sql = new System.Text.StringBuilder();
           sql.Append("INSERT INTO [NewsClass]");
           sql.Append("([NCId],[NCTitle],[NCIsShow])");
           sql.Append("VALUES");
           sql.Append("(@NCId,@NCTitle,@NCIsShow)");
           return DbHelper.ExcuteNoQuery(sql.ToString(), GetParameters(model));
       }
       // /// <summary>
       // /// 根据id删除此行数据
       // /// </summary>
       // /// <param name="id">id值</param>
       // /// <returns>返回删除的行数</returns>
       //public int delSingle(int id) {
       //    int j =0;
       //    //判断是否有子目录
       //    string sqlstr = "select * from NewsClass where ParentId = " + id;
       //    DataTable dt = DbHelper.ExcuteQuery(sqlstr);
       //    //如果有子目录，执行自身迭代，删除其子目录
       //    if (dt.Rows.Count >= 1)
       //    {
       //        foreach (DataRow dr in dt.Rows)
       //        {
       //            j += delSingle(Convert.ToInt32(dr["id"]));
       //        }
       //    }
       //        string sqlDel = "delete from NewsClass where id = " + id;
       //        j += DbHelper.ExcuteNoQuery(sqlDel);
       //    return j;
       //}
       public int delSingle(string id) {
           int j = 0;
           string sqlDel = "delete from NewsClass where NCId = @id ";
           SqlParameter pr = new SqlParameter("@id",id);
           j += DbHelper.ExcuteNoQuery(sqlDel,pr);
           return j;
       }

       /// <summary>
       /// 获取参数
       /// </summary>
       /// <param name="model">实体类</param>
       /// <returns></returns>
       private SqlParameter[] GetParameters(BoNewsClass model)
       {
           SqlParameter[] parameters = new SqlParameter[] {
                new SqlParameter("@NCId",SqlDbType.NVarChar,50),
                new SqlParameter("@NCTitle",SqlDbType.NVarChar,50),
                new SqlParameter("@NCIsShow",SqlDbType.Bit,50),
            };
           parameters[0].Value = model.NCId;
           parameters[1].Value = model.NCTitle;
           parameters[2].Value = model.NCIsShow;
           return parameters;
       }
        
       
    }
}
